package componentes;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Random;

import juego.Arkanoid;

import scenes.Nivel;

import Calculos.CalculadoraDeVersor;
import Calculos.VersorCalculable;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.appearances.Circle;

public class Bolita extends GameComponent<Nivel> implements VersorCalculable{
	
	private int radius;


	private double speed;
	private double velocidadNormal = 400;
	private double sentidoEnX, sentidoEnY;
	private Nivel nivel;
	
	public Bolita(Color baseColor, Nivel nivel) {
		this.radius = 4;
		this.speed = this.velocidadNormal;
		this.setAppearance(new Circle(baseColor, 2 * this.radius));
		this.nivel = nivel;
	}

	
	@Override
	public void onSceneActivated() {
		this.setX(250);
		this.setY(350);
		
		double sentidoEnX = 150;
		double sentidoEnY = -150;
		double m = Math.sqrt(sentidoEnX * sentidoEnX + sentidoEnY * sentidoEnY);

		sentidoEnX = sentidoEnX / m;
		sentidoEnY = sentidoEnY / m;
		this.setVersor(sentidoEnX, sentidoEnY);

		super.onSceneActivated();
	}

	@Override
	public void update(DeltaState deltaState) {
		double advanced = this.speed * deltaState.getDelta();
		this.nivel.chequeaSiChoqueConAlgo(this);
		this.move(this.sentidoEnX * advanced, this.sentidoEnY * advanced);
		super.update(deltaState);
	}

	public void cambiaDireccionHorizontal() {
		this.sentidoEnX = this.sentidoEnX * -1;
		this.corregirPosicionHorizontal(7);
	}

	private void corregirPosicionHorizontal(int corrimiento) {
		if(this.sentidoEnX < 0){
			this.setX(this.getX() - corrimiento);
		}
		else{
			this.setX(this.getX() + corrimiento);
		}
		
	}

	public void cambiaDireccionVertical(){
		this.sentidoEnY = this.sentidoEnY * -1;
		this.corregirPosicionVertical(7);
	}
	
	
	private void corregirPosicionVertical(int corrimiento) {
		if(this.sentidoEnY < 0){
			this.setY(this.getY() - corrimiento);
		}
		else{
			this.setY(this.getY() + corrimiento);
		}
		
	}

	public int getRadius() {
		return radius;
	}

	public void setRadius(int radius) {
		this.radius = radius;
	}


	@Override
	public void setVersor(double x, double y) {
		this.sentidoEnX = x;
		this.sentidoEnY = y;
	}
	

	public void reducirVelocidad() {
		this.speed = 60;
	}
	
	public void aumentarVelocidad(){
		this.speed = 1000;
	}
	
	public void normalizarVelocidad(){
		this.speed = this.velocidadNormal;
	}

}
